package card

import (
	"math/rand"
	"time"
)

type FlowerType int

const (
	//方块
	DIAMOND FlowerType = iota
	//梅花
	PLUM
	//红桃
	HEART
	//黑桃
	SPADE
)

type Card struct {
	//牌面点数，A 2 3 4 5 6 7 8 9 10 J Q K 小王 大王 -> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
	Point  int        `json:"point"`
	Flower FlowerType `json:"flower"` //扑克花色，当牌为大王或小王时该值无意义
}

// 洗牌
func confusion(cards []Card) []Card {
	//rand.Seed(time.Now().Unix())
	newCards := make([]Card, len(cards))
	index := 0
	for {
		if len(cards) <= 0 {
			break
		} else {
			rand.Seed(time.Now().UnixNano())
			randData := rand.Intn(len(cards))
			newCards[index] = cards[randData]
			index++
			cards = append(cards[:randData], cards[randData+1:]...)
		}
	}
	return newCards
}

// 生成一副不含大小王的扑克，内部会自动洗牌
func GenCards52() []Card {
	cards := make([]Card, 52)
	index := 0
	for point := 1; point < 14; point++ {
		for flower := 0; flower < 4; flower++ {
			cards[index].Point = point
			cards[index].Flower = FlowerType(flower)
			index++
		}
	}
	return confusion(cards)
}

//生成一副含大小王的扑克，内部会自动洗牌
func GenCards54() []Card {
	cards := make([]Card, 52)
	index := 0

	for point := 1; point < 14; point++ {
		for flower := 0; flower < 4; flower++ {
			cards[index].Point = point
			cards[index].Flower = FlowerType(flower)
			index++
		}
	}
	//加入大小王
	cards = append(cards, Card{Point: 14}, Card{Point: 15})
	return confusion(cards)
}
